package com.iuie.ns.devtools.transfer;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.iuie.ns.devtools.element.BaseDevElement;
import com.iuie.ns.devtools.element.DevElement;
import com.iuie.ns.devtools.to.DataSetDevHeaderTO;
import com.iuie.ns.devtools.to.DevBodyTO;
import com.iuie.ns.devtools.to.DevHeaderTO;
import com.iuie.ns.frame.utils.ListUtils;
import com.iuie.ns.frame.utils.StringUtils;

@Component("dataSetTransfer")
public class DataSetDevTransfer extends BaseDevTransfer {
	
	@Override
	public String transfer(List<? extends DevBodyTO> list, DevHeaderTO header) {
		DataSetDevHeaderTO mHeader = (DataSetDevHeaderTO) header;
		List<DevElement> mlist = new ArrayList<>();
		if (ListUtils.isNotEmpty(list)) {
			for (DevBodyTO body: list) {
				StringBuilder sb = new StringBuilder(12);
				sb.append("\n\t<" + mHeader.getTag() + "dataField")
					.append(" name=\"" + body.getAttributeName() + "\"");
				if (StringUtils.isBlank(body.getDisplay())) {
					sb.append(" display=\"" + body.getComment() + "\"");
				} else {
					sb.append(" display=\"" + body.getDisplay() + "\"");
				}
				if (StringUtils.equals(mHeader.getTag(), "tags:")) {
					sb.append(" cssClass=\"table-header-center\"");
				}
				sb.append("/>");
				DevElement element = new BaseDevElement(sb.toString());
				mlist.add(element);
			}
		}
		return new BaseDevElement("<" + mHeader.getTag() + "dataSet id=\"" + mHeader.getSimpleClassName() + "Set\">", mlist, 
				"\n</" + mHeader.getTag() + "dataSet>").build();
	}
	

}
